package com.gton.config.filter;

import com.alibaba.fastjson.JSON;
import com.gton.config.util.NanoIdUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.InetSocketAddress;

/**
 * @description: 全局接入请求过滤器|全局日志打印
 * @author: GuoTong
 * @createTime: 2022-10-18 20:41
 * @since JDK 1.8 OR 11
 **/
@Component
@Slf4j
public class URlGlobalUseFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        log.info("网关：Gateway 接入了请求：{},", request.getURI());
        InetSocketAddress remoteAddress = request.getRemoteAddress();
        MultiValueMap<String, String> queryParams = request.getQueryParams();
        log.info("网关请求参数：{}", JSON.toJSONString(queryParams));
        assert remoteAddress != null;
        String hostAddress = remoteAddress.getAddress().getHostAddress();
        log.info("进入远程主机：{}", hostAddress);
        //拦截器链本次放行
        return chain.filter(exchange);
    }


    /**
     * Description: 设置过滤器的优先级，值越小，优先级越高
     *
     * @author: GuoTong
     * @date: 2022-10-18 20:43:07
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
